package net;

/** A command that can be parsed from a received message or transformed to a message to be sent.
 * A command consists of a header and a body. The header is a 4 character long string.
 * The body can is a string of undefined length. 
 * Example: HEAD arg1 arg2 arg3
 */
public class NetCommand {

	private String _head;
	private String _body;
	private final static String _delimiter = " ";
	private final static int _headLength = 4;
	
	public NetCommand(String head, String body){
		_head = head;
		_body = body;
	}
	
	public static NetCommand valueOf(NetMessage message){
		String str = message.toString();
		String head = str.substring(0, _headLength);
		String body = str.length() > 5 ? str.substring(_headLength + _delimiter.length()) : "";
		return new NetCommand(head, body);
	}
	
	public String toString(){
		return _head + _delimiter + _body;
	}
	
	public NetMessage toMessage(){
		return new NetMessage(toString());
	}
	
	public byte[] getBytes() {
		return toString().getBytes();
	}
	
	public String getHead() { 
		return _head;
	}

	public String getBody() {
		return _body;
	} 

}
